<template>
   <message-content-box title="我的评论与回复">
      <template #header>
         <div @click="oneClickRead">一键已读</div>
      </template>
      <template #content>
         <div>
            <ul>
               <li v-for="(item, index) in state.commentReplyList" :key="index" @mouseenter="showdelButton = index"
                  @mouseleave="showdelButton = -1">
                  <!-- 评论类型 -->
                  <div v-if="item.type === 'comment'" class="item">
                     <div class="commentreply-left">
                        <div class="left-top">
                           <div class="image" @click="Utils.viewAuthor(item.sender.id)">
                              <el-image :src="UtilsImage(item.sender.useravatar)" fit="cover" lazy />
                           </div>
                           <div class="name-content">
                              <div class="name">
                                 <span @click="Utils.viewAuthor(item.sender.id)">
                                    {{ item.sender.username }}
                                 </span>：&nbsp;评论了我的 &nbsp;[&nbsp;
                                 <span class="span"
                                    @click="goPostsComment(item.postsEventId.postsId, item.commentEventId._id, item._id)">
                                    {{ item.postsEventId.postsTitle || item.postsEventId.postsContent.slice(0, 20) }}
                                 </span>&nbsp;]
                              </div>
                              <p @click="goPostsComment(item.postsEventId.postsId, item.commentEventId._id, item._id)">
                                 {{ item.commentEventId.commentContent }}
                              </p>
                           </div>
                        </div>
                        <div class="left-bottom">
                           <span class="span">{{ Utils.timeDisplay(item.createdAt) }}</span>
                           <span class="span">
                              <span style="margin: 5px;" class="iconfont icon-pinglun"></span>回复
                           </span>
                           <span class="span">
                              <span style="margin: 5px;" class="iconfont icon-dianzan"></span>点赞
                           </span>
                           <span class="span" v-if="showdelButton === index"
                              @click="delNotification(item._id)">删除该通知</span>
                        </div>
                     </div>
                     <div class="commentreply-right">
                        <div class="btn" v-if="item.status === false"
                           @click="goPostsComment(item.postsEventId.postsId, item.commentEventId._id, item._id)">
                           NEW
                        </div>
                        <div class="btn"
                           @click="goPostsComment(item.postsEventId.postsId, item.commentEventId._id, item._id)">
                           查看
                        </div>
                     </div>
                  </div>
                  <!-- 回复类型 -->
                  <div v-if="item.type === 'reply'" class="item">
                     回复
                  </div>
               </li>
               <el-empty v-if="state.commentReplyList.length === 0" description="暂没有消息" />
            </ul>
            <div class="paging" v-if="state.commentReplyTotal > 0">
               <el-pagination v-model:currentPage="state.page" v-model:page-size="state.limit" layout="prev, pager, next"
                  :total="state.commentReplyTotal" @size-change="sizeChange" @current-change="currentChange" />
            </div>
         </div>
      </template>
   </message-content-box>
</template>
<script lang="ts" setup >
import { storeToRefs } from 'pinia';
import { ElMessage } from 'element-plus';
const { userinfo, notificationNumber } = storeToRefs(cnyzsUserpinia())
const showdelButton = ref(0)

const state: any = ref({
   commentReplyList: [],   // 评论与回复消息通知列表
   commentReplyTotal: 0,   // 总数
   page: 1,    // 页码
   limit: 4,   // 每页数量
})

// 一键已读
const oneClickRead = () => {
   ElMessage.success('开发中...')
}

// 阅读评论并跳转到相关页面
const goPostsComment = async (id: any, commentId: string, _id?: any) => {
   window.open(id + '?commentid=' + commentId, '_blank')
   read(_id)
}

// 删除消息通知
const delNotification = async (id: string) => {
   const res: any = await cnyzsRequest('/api/notification/del-comment-reply', {
      method: 'POST',
      data: {
         notificationId: id,
      },
   })
   if (res.status !== 200) return ElMessage.warning(res.message)
   ElMessage.success(res.message)
   getCommentReply()
}

// 阅读消息通知
const read = async (_id: any) => {
   const res: any = await cnyzsRequest('/api/notification/read-comment-reply', {
      method: 'POST',
      data: { notificationId: _id }
   })
   if (res.status === 200) {
      if (notificationNumber.value <= 0) return
      // 修改当前数据状态
      state.value.commentReplyList.forEach((n: any, index: number) => {
         if (_id === n._id) {
            state.value.commentReplyList[index].status = true
         }
      });
      // 修改未读消息总数
      notificationNumber.value = notificationNumber.value - 1
   }
}

// 获取评论回复通知
const getCommentReply = async () => {
   const res: any = await cnyzsRequest('/api/notification/get-comment-reply', {
      method: 'POST',
      data: {
         page: state.value.page,
         limit: state.value.limit,
      }
   })
   if (res.status === 200) {
      state.value.commentReplyList = res.data.commentReplyList
      state.value.commentReplyTotal = res.data.commentReplyTotal
   }
}
await getCommentReply()

// 分页
const sizeChange = async (val: number) => {
   state.value.limit = val // 改变每页显示条数
   await getCommentReply()
}
const currentChange = async (val: number) => {
   state.value.page = val // 改变当前页
   await getCommentReply()
}


</script>
<style lang="scss" scoped>
.message-content-box {
   width: 100%;
   min-height: 730px;
   background-color: #FFF;
   border-radius: var(--aoir-border-radius);
   box-shadow: var(--aoir-box-shadow);
   padding: 20px;


   ul {
      width: 100%;

      li {
         width: 100%;
         border-radius: var(--aoir-border-radius);
         padding: 18px 18px 15px;
         margin-bottom: 15px;
         border-bottom: 1px solid #EEE;
         transition: all .2s;

         &:hover {
            background-color: #eeeeee5a;
         }

         .item {
            width: 100%;
            height: 100%;
            display: flex;
            justify-content: space-between;

            .commentreply-left {
               flex: 1;
               display: flex;
               flex-direction: column;

               .left-top {
                  flex: 1;
                  display: flex;

                  .image {
                     width: 42px;
                     height: 42px;
                     border-radius: 50%;
                     margin-right: 15px;
                     overflow: hidden;
                     border: 2px solid #a1a1a1;
                     cursor: pointer;

                     .el-image {
                        width: 100%;
                        height: 100%;
                     }
                  }

                  .name-content {
                     flex: 1;
                     display: flex;
                     flex-direction: column;

                     .name {
                        width: fit-content;
                        display: flex;
                        font-size: 15px;
                        cursor: pointer;

                        .span {
                           color: #999;

                           &:hover {
                              color: var(--aoir-color-primary);
                              cursor: pointer;
                           }
                        }
                     }

                     p {
                        width: fit-content;
                        line-height: 25px;
                        margin: 12px 0;
                        padding: 6px 12px;
                        background: #eeeeee8a;
                        border-radius: 10px;
                        cursor: pointer;
                     }
                  }
               }

               .left-bottom {
                  padding-left: 58px;
                  display: flex;
                  align-items: center;

                  .span {
                     color: #AAA;
                     font-size: 14px;
                     margin-right: 15px;
                     cursor: pointer;

                     &:hover {

                        span {
                           color: var(--aoir-color-primary);
                        }

                        color: var(--aoir-color-primary);
                     }
                  }
               }
            }

            .commentreply-right {
               width: 130px;
               margin-left: 20px;
               display: flex;
               align-items: center;
               justify-content: flex-end;

               .btn {
                  margin-right: 15px;
                  border-radius: 10px;
                  background-color: var(--aoir-color-primary);
                  padding: 4px 8px;
                  color: #FFF;
                  font-size: 12px;
                  font-weight: 600;
                  cursor: pointer;
                  transition: all 0.3s;

                  &:hover {
                     opacity: .7;
                  }

                  &:nth-last-child(1) {
                     margin: 0;
                     background-color: #575757;
                  }
               }

               .read {
                  background-color: #b4b4b4;
               }
            }
         }
      }
   }

   .paging {
      width: 100%;
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: center;
   }
}

:deep(.el-pager li.is-active) {
   color: #FFF;
   cursor: default;
   font-weight: 700;
   background: var(--aoir-color-primary);
   border-radius: 50%;
}
</style>